package com.xxx.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.mall.constant.EnableStatusEnum;
import com.xxx.mall.constant.IsDeletedEnum;
import com.xxx.mall.converter.BrandConverter;
import com.xxx.mall.dto.BrandDTO;
import com.xxx.mall.dto.BrandQuery;
import com.xxx.mall.dto.PageQuery;
import com.xxx.mall.exception.MallException;
import com.xxx.mall.mapper.BrandMapper;
import com.xxx.mall.model.Brand;
import com.xxx.mall.service.BrandService;
import com.xxx.mall.service.CategoryBrandService;
import com.xxx.mall.service.CategoryService;
import com.xxx.mall.vo.BrandVO;
import com.xxx.mall.vo.CategoryVO;
import com.xxx.mall.vo.PageVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 品牌信息 服务实现类
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-11-15
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Resource
    BrandConverter brandConverter;

    @Resource
    CategoryBrandService categoryBrandService;

    @Resource
    CategoryService categoryService;

    @Override
    public PageVO<BrandVO> pageList(PageQuery pageQuery, BrandQuery brandQuery) {
        return PageVO.of(baseMapper.queryPage(PageDTO.of(pageQuery.getPage(), pageQuery.getSize()), brandQuery));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBrand(BrandDTO brandDTO) {
        Brand brand = brandConverter.toBrand(brandDTO);
        brand.setStatus(EnableStatusEnum.DISABLE.value());
        brand.setIsDeleted(IsDeletedEnum.NOT_DELETED.value());
        //保存品牌信息
        baseMapper.insert(brand);
        //保存关联信息
        categoryBrandService.saveByCategoryIds(brand.getBrandId(), brandDTO.getCategoryIds());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBrand(BrandDTO brandDTO) {
        baseMapper.updateById(brandConverter.toBrand(brandDTO));
        categoryBrandService.updateByCategoryIds(brandDTO.getBrandId(), brandDTO.getCategoryIds());
    }

    @Override
    public BrandVO queryById(Long brandId) {
        //查询品牌信息
        Brand brand = baseMapper.selectById(brandId);
        if (brand == null || IsDeletedEnum.DELETED.value().equals(brand.getIsDeleted())) {
            throw new MallException("品牌不存在");
        }
        BrandVO brandVO = brandConverter.toBrandVO(brand);
        //查询关联的分类信息
        List<CategoryVO> categoryList = categoryBrandService.queryCategoryByBrandId(brandId);
        brandVO.setCategories(categoryList);
        return brandVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long brandId) {
        //TODO 判断是否有商品在使用该品牌
        //TODO 启用状态的品牌不能删除
        baseMapper.removeById(brandId);
        categoryBrandService.removeByBrandId(brandId);
    }

    @Override
    public PageVO<BrandVO> pageList(PageQuery pageQuery, Long categoryId) {
        List<Long> ids = categoryService.queryAllParentId(categoryId);
        return PageVO.of(baseMapper.queryPageByCategoryId(PageDTO.of(pageQuery.getPage(), pageQuery.getSize()), ids));
    }
}
